package com.itheima.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.itheima.entity.Result;
import com.itheima.entity.StatusCode;
import com.itheima.pojo.Notice;
import com.itheima.pojo.NoticeFresh;
import com.itheima.service.NoticeService;
import com.itheima.utile.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    NoticeService noticeService;
    @Autowired
    IdWorker idWorker;
    @GetMapping("/{id}")
    public Result get(@PathVariable("id") String id){
        Notice notice = noticeService.get(id);
        return new Result(true, StatusCode.OK,"查询成功",notice);
    }
    @PostMapping("/{page}/{size}")
    public Result get(@RequestBody Notice notice, @PathVariable("page") String page, @PathVariable("size") String size){
        Page<Notice>  noticePage = noticeService.pageGet(notice,page,size);
        return new Result(true, StatusCode.OK,"查询成功",noticePage);
    }
    @PostMapping
    public Result add(@RequestBody Notice notic){
       noticeService.save(notic,idWorker);
        return new Result(true, StatusCode.OK,"新增成功");
    }
    @PutMapping
    public Result update(@RequestBody Notice notice){
        noticeService.update(notice);
        return new Result(true, StatusCode.OK,"修改成功",notice);
    }
    @GetMapping("/fresh/{userId}")
    public Result findFreshMessage(@PathVariable("userId") String userId){
        List<NoticeFresh> list=noticeService.findFreshMessage(userId);
        return new Result(true, StatusCode.OK,"查询成功",list);
    }
    @DeleteMapping
    public Result deleteNoticeFresh(@RequestBody NoticeFresh noticeFresh){
        noticeService.deleteNoticeFresh(noticeFresh);
        return new Result(true, StatusCode.OK,"删除成功");
    }
}
